/*
 * Copyright 2011 David de Mingo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.shalma.internal.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Attribute {

	private final String name;
	private Class<?> type;
	private Method reader;
	private Method writer;

	public Attribute(String name) {
		this.name = name;
	}

	public void setReader(Method method) {
		reader = method;
		type = method.getReturnType();
	}

	public void setWriter(Method method) {
		writer = method;
	};

	public String getName() {
		return name;
	}

	public Class<?> getType() {
		return type;
	}

	public Object read(Object object) throws IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {
		return reader.invoke(object);
	}

	public boolean isReadOnly() {
		return writer == null;
	}

	public void set(Object object, Object value) {
		throw new UnsupportedOperationException();
	}

	@Override
	public String toString() {
		return name;
	}

}
